﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using UserLicense.Applicationes.Command;
using UserModels;
using UserRepositorys.Intface;

namespace UserLicense.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly IRoleRepository roleRepository;
        private readonly ITiesRepository tiesRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public RoleController(IMediator mediator,IRoleRepository roleRepository,ITiesRepository tiesRepository)
        {
            this.mediator = mediator;
            this.roleRepository = roleRepository;
            this.tiesRepository = tiesRepository;
        }

        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="roleAdd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddRole(RoleAddCommand roleAdd)
        {
            return await mediator.Send(roleAdd);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="roleDelete"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<int> DeleteRole(RoleDeleteCommand roleDelete)
        {
            return await mediator.Send(roleDelete);
        }

        /// <summary>
        /// 查询所有角色信息
        /// </summary>
        /// <param name="Route"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ShowRole(RoleListCommand Route)
        {
            var list= await mediator.Send(Route);
           return  Ok(new { list.totalCount, list.pageCount, list.roleList });
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="tiesAdd"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<int> AddTies(TiesAddCommand tiesAdd)
        {
            return await mediator.Send(tiesAdd);
        }

        /// <summary>
        /// 角色信息全部
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ObjectResult> GetRoleShow(string? roleName, int pageIndex, int pageSize)
        {
          var list= await roleRepository.GetRoleShow(roleName, pageIndex, pageSize);
            return Ok(new { list.totalCount, list.pageCount, list.roleList });
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> DeleteRoleMune(int id)
        {
            return await tiesRepository.DeleteRoleMune(id);
        }
    }
}
